#!/usr/bin/env python
import sys, os, glob, codecs
from pprint import pprint

sys.path.insert(0, os.path.join(os.path.abspath(os.curdir), "libs", "jinja"))

from jinja2 import Environment

env = Environment(extensions=['jinja2.ext.i18n'])

out = codecs.open('./locale/planetsuse.pot', 'wb', encoding='utf8')

if False:
    print >>out, u"""\
msgid ""
msgstr ""
"Project-Id-Version: 0\\n"
"Report-Msgid-Bugs-To: pascal.bleser@opensuse.org\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=UTF-8\\n"
"Content-Transfer-Encoding: 8bit\\n"

"""

map = {}
map_order = []

for filename in glob.glob('planetsuse/*.html'):
    file = codecs.open(filename, 'rb', encoding='utf8')
    content = file.read()
    file.close()
    ast = env.parse(content)
    for (lineno, function, message) in env.extract_translations(ast):
        if not message in map:
            map[message] = []
            map_order.append(message)
            pass
        map[message].append((filename, lineno))
        pass
    pass

for message in map_order:
    location_tuples = map[message]
    locations = " ".join([":".join([str(x) for x in t]) for t in location_tuples])
    print >>out, u"#: " + locations
    print >>out, u"msgid \"%s\"" % message.replace("\"", "\\\"")
    print >>out, u"msgstr \"\""
    print >>out
    pass

out.close()

